<template>
  <a-drawer
    :title="title"
    :maskClosable="true"
    :width="drawerWidth"
    placement="right"
    :closable="true"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @close="handleCancel"
    style="height: 100%; overflow: auto; padding-bottom: 53px"
  >
    <template slot="title">
      <div style="width: 100%">
        <span>{{ title }}</span>
        <span style="display: inline-block; width: calc(100% - 51px); padding-right: 10px; text-align: right">
          <a-button @click="toggleScreen" icon="appstore" style="height: 20px; width: 20px; border: 0px"></a-button>
        </span>
      </div>
    </template>

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serviceName" required label="服务名称">
          <a-input placeholder="请输入服务名称" v-model="model.serviceName" :maxLength="30" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serviceCategoryId" label="服务分类">
          <a-select
            v-model="model.serviceCategoryId"
            placeholder="请选择服务分类"
            :getPopupContainer="target => target.parentNode"
          >
            <a-select-option :value="item.dataId" v-for="item in serviceCategory" :key="item.dataId">{{
              item.categoryName
            }}</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="executorIds" label="执行角色">
          <a-select
            v-model="model.executorIds"
            placeholder="请选择执行角色"
            :getPopupContainer="target => target.parentNode"
          >
            <a-select-option :value="item.value" v-for="item in executorIds" :key="item.value">{{
              item.label
            }}</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="品牌/设备" prop="serviceBrand">
          <a-input placeholder="请输入品牌/设备" v-model="model.serviceBrand" :maxLength="20" />
        </a-form-model-item>
        <!--<a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="servicePrice"
          label="单项零售价">
          <a-input-number placeholder="请输入单项零售价" v-model="model.servicePrice" style="width: 100%"/>
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="serviceDiscount"
          label="折扣价">
          <a-input-number placeholder="请输入折扣价" v-model="model.serviceDiscount" style="width: 100%"/>-->
        <!-- </a-form-model-item> -->
        <a-form-model-item label="服务图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="servicePhoto">
          <j-image-upload
            class="avatar-uploader"
            text="上传"
            v-model="model.servicePhoto"
            :is-multiple="true"
          ></j-image-upload>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serviceDesc" label="服务描述">
          <j-editor v-model="model.serviceDesc" :max-length="500" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>

    <div class="drawer-bootom-button" v-show="!disableSubmit">
      <a-popconfirm title="确定放弃编辑？" @confirm="handleCancel" okText="确定" cancelText="取消">
        <a-button style="margin-right: 0.8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleOk" type="primary" :loading="confirmLoading">提交</a-button>
    </div>
  </a-drawer>
</template>

<script>
import Vue from 'vue'
import { USER_INFO } from '@/store/mutation-types'
import { getAction, postAction } from '@/api/manage'
import { mapGetters } from 'vuex'

export default {
  name: 'ServiceModal',
  data() {
    return {
      drawerWidth: 900,
      disableSubmit: false,
      value: 1,
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        serviceName: [{ required: true, message: '请输入服务名称!' }],
        serviceCategoryId: [{ required: true, message: '请选择服务分类!' }],
        executorIds: [{ required: true, message: '请选择执行角色!' }],
        serviceBrand: [{ required: true, message: '请输入品牌/设备!' }],
        servicePrice: [{ required: true, message: '请输入单项零售价!' }],
        serviceDesc:[{ required: true, message: '请输入服务描述!' }]
      },
      serviceCategory: [],
      executorIds: [
        {
          label: '医生',
          value: '1'
        },
        {
          label: '营养师',
          value: '2'
        },
        {
          label: '健管师',
          value: '3'
        },
        {
          label: '心理咨询师',
          value: '4'
        },
        {
          label: '运动康复师',
          value: '5'
        },
        {
          label: '责任医生',
          value: '6'
        },
        {
          label: '主任医生',
          value: '7'
        }
      ]
    }
  },
  watch: {
    'model.serviceDesc'(n) {
      console.log(n)
      if (n.length > 500) {
        this.model.serviceDesc = n.substring(0, 500)
      }
    }
  },
  created() {},
  methods: {
    ...mapGetters(['doctorId', 'orgCode']),
    add() {
      this.edit({ dataId: null })
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
      this.handleGetServiceCategory()
      this.handleGetExecutors()
      this.resetScreenSize()
    },
    handleGetExecutors() {
      getAction('107/03/1070319', {doctorId: this.doctorId(), orgCode: this.orgCode()}).then(res => {
        if (res.success) {
          this.executorIds = res.result
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    handleGetServiceCategory() {
       getAction('107/03/1070306', {doctorId: this.doctorId(), orgCode: this.orgCode()}).then(res => {
        if (res.success) {
          this.serviceCategory = res.result
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth
      if (screenWidth < 500) {
        this.drawerWidth = screenWidth
      } else {
        this.drawerWidth = 900
      }
    },
    //窗口最大化切换
    toggleScreen() {
      if (this.modaltoggleFlag) {
        this.modalWidth = window.innerWidth
      } else {
        this.modalWidth = 800
      }
      this.modaltoggleFlag = !this.modaltoggleFlag
    },
    // 确定
    handleOk() {
      const that = this
      // 触发表单验证
      that.$refs.form.validate(valid => {
        if (valid) {
          this.model = Object.assign(this.model, {doctorId: this.doctorId(), orgCode: this.orgCode()})
          that.confirmLoading = true
          postAction('107/03/1070301', that.model)
            .then(res => {
              console.log('res=======', res)
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    // 关闭
    handleCancel() {
      this.close()
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.disableSubmit = false
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style scoped>
.avatar-uploader > .ant-upload {
  width: 104px;
  height: 104px;
}
.ant-upload-select-picture-card i {
  font-size: 49px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}

.ant-table-tbody .ant-table-row td {
  padding-top: 10px;
  padding-bottom: 10px;
}

.ant-spin-nested-loading{
  padding-bottom: 53px;
}

.drawer-bootom-button {
  position: absolute;
  bottom: -8px;
  width: 100%;
  border-top: 1px solid #e8e8e8;
  padding: 10px 16px;
  text-align: right;
  left: 0;
  background: #fff;
  border-radius: 0 0 2px 2px;
}
</style>

